﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLCV
{
    public partial class frmTraCuuDi : Form
    {

        CongVanDiSearchDTO _congVanDiSearchDTO = new CongVanDiSearchDTO();
        public frmTraCuuDi()
        {
            InitializeComponent();
        }

        void DateTimePicker_Enable(DateTimePicker dtp)
        {
            dtp.CustomFormat = "d/M/yyyy";
            dtp.Enabled = true;
        }

        void DateTimePicker_Disable(DateTimePicker dtp)
        {
            dtp.CustomFormat = " ";
            dtp.Enabled = false;
        }


        private void frmTraCuuDi_Load(object sender, EventArgs e)
        {
            //theo ngay luu
            rbtnTatCa.Checked = true;
            //an cac nut khac
            ckbDenNgay.Enabled = false;
            ckbDenNgay.Checked = false;
            ckbTuNgay.Enabled = false;
            ckbTuNgay.Checked = false;

            //theo han xu ly
            rbtnTatCa_NK.Checked = true;
            //an cac nut của phan này
            ckbDenNgay_NK.Enabled = false;
            ckbDenNgay_NK.Checked = false;
            ckbTuNgay_NK.Enabled = false;
            ckbTuNgay_NK.Checked = false;

            //load hệ thống , đơn vị , loại và người tiếp nhận xử lý
            cmbHeThong.DisplayMember = "Ten";
            cmbHeThong.ValueMember = "ID";
            List<HeThongDTO> tmpHeThong = HeThongBUS.LoadAll();
            tmpHeThong.Insert(0, new HeThongDTO(0, "<Tìm tất cả>"));
            cmbHeThong.DataSource = tmpHeThong;




            List<NoiNhanDTO> tmpNoiNhan = new List<NoiNhanDTO>();
            tmpNoiNhan.Insert(0, new NoiNhanDTO(0, "<Tìm tất cả>", null, 0));
            cmbNoiNhan.DataSource = tmpNoiNhan;
            cmbNoiNhan.DisplayMember = "Ten";
            cmbNoiNhan.ValueMember = "ID";

            List<LoaiDiDTO> tmpLoaiDi = LoaiDiBUS.LoadAll();
            tmpLoaiDi.Insert(0, new LoaiDiDTO(0, "<Tìm tất cả>"));
            cmbLoai.DataSource = tmpLoaiDi;
            cmbLoai.DisplayMember = "Ten";
            cmbLoai.ValueMember = "ID";

            List<NguoiDTO> tmpNguoi = NguoiBUS.LoadAll();
            tmpNguoi.Insert(0, new NguoiDTO(0, "<Tìm tất cả>", false));
            cmbNguoiKy.DataSource = tmpNguoi;
            cmbNguoiKy.DisplayMember = "Ten";
            cmbNguoiKy.ValueMember = "ID";

            btnChinhSua.Enabled = false;
            btnXemChiTiet.Enabled = false;
            btnLuu.Enabled = false;



        }

        private void rbtnTrongNgay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTrongNgay.Checked)
                {
                    DateTimePicker_Enable(dtpTrongNgay);

                    _congVanDiSearchDTO.NgayLuuMax = dtpTrongNgay.Value;
                    _congVanDiSearchDTO.NgayLuuMin = dtpTrongNgay.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpTrongNgay);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTrongKhoang_CheckedChanged(object sender, EventArgs e)
        {

            try
            {
                if (rbtnTrongKhoang.Checked)
                {
                    ckbDenNgay.Enabled = true;
                    ckbDenNgay.Checked = true;
                    ckbTuNgay.Enabled = true;
                    ckbTuNgay.Checked = true;

                    _congVanDiSearchDTO.NgayLuuMax = dtpDenNgay.Value;
                    _congVanDiSearchDTO.NgayLuuMin = dtpTuNgay.Value;
                }
                else
                {
                    ckbDenNgay.Enabled = false;
                    ckbDenNgay.Checked = false;
                    ckbTuNgay.Enabled = false;
                    ckbTuNgay.Checked = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbTuNgay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbTuNgay.Checked)
                {
                    DateTimePicker_Enable(dtpTuNgay);
                    if (ckbDenNgay.Checked && dtpTuNgay.Value > dtpDenNgay.Value)
                    {
                        dtpTuNgay.Value = dtpDenNgay.Value;
                    }
                    _congVanDiSearchDTO.NgayLuuMin = dtpTuNgay.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpTuNgay);
                    _congVanDiSearchDTO.NgayLuuMin = null;
                    //dtpDenNgay.MinDate = new DateTime(1973, 1, 1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbDenNgay_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbDenNgay.Checked == true)
                {
                    DateTimePicker_Enable(dtpDenNgay);
                    if (ckbTuNgay.Checked && dtpTuNgay.Value > dtpDenNgay.Value)
                    {
                        dtpDenNgay.Value = dtpTuNgay.Value;
                    }
                    _congVanDiSearchDTO.NgayLuuMax = dtpDenNgay.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpDenNgay);
                    _congVanDiSearchDTO.NgayLuuMax = null;
                    //dtpTuNgay.MaxDate = new DateTime(9990, 12, 31);;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTatCa_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTatCa.Checked)
                {
                    _congVanDiSearchDTO.NgayLuuMax = null;
                    _congVanDiSearchDTO.NgayLuuMin = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTatca_NK_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTatCa_NK.Checked)
                {
                    _congVanDiSearchDTO.NgayKyMin = null;
                    _congVanDiSearchDTO.NgayKyMax = null;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTrongNgay_NK_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTrongNgay_NK.Checked)
                {
                    DateTimePicker_Enable(dtpTrongNgay_NK);

                    _congVanDiSearchDTO.NgayKyMax = dtpTrongNgay_NK.Value;
                    _congVanDiSearchDTO.NgayKyMin = dtpTrongNgay_NK.Value;

                }
                else
                {
                    DateTimePicker_Disable(dtpTrongNgay_NK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rbtnTrongKhoang_NK_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (rbtnTrongKhoang_NK.Checked)
                {
                    ckbDenNgay_NK.Enabled = true;
                    ckbDenNgay_NK.Checked = true;
                    ckbTuNgay_NK.Enabled = true;
                    ckbTuNgay_NK.Checked = true;

                    dtpTrongNgay_NK.Enabled = false;

                    _congVanDiSearchDTO.NgayKyMax = dtpDenNgay_NK.Value;
                    _congVanDiSearchDTO.NgayKyMin = dtpTuNgay_NK.Value;
                }
                else
                {
                    ckbDenNgay_NK.Enabled = false;
                    ckbDenNgay_NK.Checked = false;
                    ckbTuNgay_NK.Enabled = false;
                    ckbTuNgay_NK.Checked = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbTuNgay_NK_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbTuNgay_NK.Checked)
                {
                    DateTimePicker_Enable(dtpTuNgay_NK);
                    if (ckbDenNgay_NK.Checked && dtpTuNgay_NK.Value > dtpDenNgay_NK.Value)
                    {
                        dtpTuNgay_NK.Value = dtpDenNgay_NK.Value;
                    }
                    _congVanDiSearchDTO.NgayKyMin = dtpTuNgay_NK.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpTuNgay_NK);
                    _congVanDiSearchDTO.NgayKyMin = null;
                    //dtpDenNgay_NK.MinDate = new DateTime(1973, 1, 1);;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ckbDenNgay_NK_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (ckbDenNgay_NK.Checked == true)
                {
                    DateTimePicker_Enable(dtpDenNgay_NK);
                    if (ckbTuNgay_NK.Checked && dtpTuNgay_NK.Value > dtpDenNgay_NK.Value)
                    {
                        dtpDenNgay_NK.Value = dtpTuNgay_NK.Value;
                    }
                    _congVanDiSearchDTO.NgayKyMax = dtpDenNgay_NK.Value;
                }
                else
                {
                    DateTimePicker_Disable(dtpDenNgay_NK);
                    _congVanDiSearchDTO.NgayKyMax = null;
                    //dtpTuNgay_NK.MaxDate = new DateTime(9990, 12, 31);;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThoatDi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChinhSuaDi_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemDi frm = new frmThemDi();
                if (dataGridView1.SelectedRows.Count != 1)
                {
                    return;
                }
                string tmp = dataGridView1["clnID", dataGridView1.CurrentRow.Index].Value.ToString();
                frm.CongVanDi = CongVanDiBUS.Load(int.Parse(tmp));
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnTraCuuDi_Click(object sender, EventArgs e)
        {
            try
            {
                lblMess.Text = "Đang tìm kiếm...";
                dataGridView1.Refresh();
                dataGridView1.ClearSelection();

                //DataTable kq = new DataTable();
                //kq = CongVanDiBUS.TraCuu(_congVanDiSearchDTO);

                dataGridView1.DataSource = CongVanDiBUS.TraCuu(_congVanDiSearchDTO);
                lblMess.Text = "Tìm thấy " + dataGridView1.RowCount.ToString() + " kết quả.";
                if (dataGridView1.RowCount == 0)
                {
                    btnLuu.Enabled = false;
                }
                else
                {
                    btnLuu.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                String pathname = "";
                SaveFileDialog savediaglog = new SaveFileDialog();
                savediaglog.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
                savediaglog.Filter = "Excel (*.xls)|*.xls|All Files (*.*)|*.*";
                savediaglog.FilterIndex = 1;

                if (savediaglog.ShowDialog() == DialogResult.OK)
                {
                    pathname = savediaglog.FileName;


                    if (pathname != "")
                    {

                        DataTable data = (DataTable)dataGridView1.DataSource;
                        if (data != null)
                        {
                            for (int i = 0; i < data.Columns.Count; i++)
                            {
                                data.Columns[i].Caption = dataGridView1.Columns[i].HeaderText;
                            }
                            data.Columns.RemoveAt(data.Columns.Count - 1);
                            CongVanDiBUS.Luu(pathname, data);
                        }
                        else
                            MessageBox.Show("Không Có Dữ Liệu ");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXemChiTietDi_Click(object sender, EventArgs e)
        {
            try
            {
                frmThemDi frm = new frmThemDi();
                if (dataGridView1.SelectedRows.Count != 1)
                {
                    return;
                }
                string tmp = dataGridView1["clnID", dataGridView1.CurrentRow.Index].Value.ToString();
                frm.CongVanDi = CongVanDiBUS.Load(int.Parse(tmp));
                frm.ReadOnly = true;
                frm.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dtpTrongNgay_ValueChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NgayLuuMax = dtpTrongNgay.Value;
            _congVanDiSearchDTO.NgayLuuMin = dtpTrongNgay.Value;
        }

        private void dtpTuNgay_ValueChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NgayLuuMin = dtpTuNgay.Value;
            if (dtpDenNgay.Value < dtpTuNgay.Value && ckbDenNgay.Checked)
            {
                dtpDenNgay.Value = dtpTuNgay.Value;
            }
            //dtpDenNgay.MinDate = dtpTuNgay.Value;
        }

        private void dtpDenNgay_ValueChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NgayLuuMax = dtpDenNgay.Value;
            if (dtpDenNgay.Value < dtpTuNgay.Value && ckbTuNgay.Checked)
            {
                dtpTuNgay.Value = dtpDenNgay.Value;
            }
            //dtpTuNgay.MaxDate = dtpDenNgay.Value;
        }

        private void dtpTrongNgay_NK_ValueChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NgayKyMax = dtpTrongNgay_NK.Value;
            _congVanDiSearchDTO.NgayKyMin = dtpTrongNgay_NK.Value;
        }

        private void dtpTuNgay_NK_ValueChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NgayKyMin = dtpTuNgay_NK.Value;
            if (dtpDenNgay_NK.Value < dtpTuNgay_NK.Value && ckbDenNgay_NK.Checked)
            {
                dtpDenNgay_NK.Value = dtpTuNgay_NK.Value;
            }
            //dtpDenNgay_NK.MinDate = dtpTuNgay_NK.Value;
        }

        private void dtpDenNgay_NK_ValueChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NgayKyMax = dtpDenNgay_NK.Value;
            if (dtpDenNgay_NK.Value < dtpTuNgay_NK.Value && ckbTuNgay_NK.Checked)
            {
                dtpTuNgay_NK.Value = dtpDenNgay_NK.Value;
            }
            //dtpTuNgay_NK.MaxDate = dtpDenNgay_NK.Value;
        }

        private void cmbHeThong_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<NoiNhanDTO> tmpNoiNhan;
            if (cmbHeThong.SelectedIndex == -1)
            {
                _congVanDiSearchDTO.HeThong = null;
                tmpNoiNhan = new List<NoiNhanDTO>();
                tmpNoiNhan.Insert(0, new NoiNhanDTO(0, "<Tìm tất cả>", null, 0));
                cmbNoiNhan.DataSource = tmpNoiNhan;
                cmbNoiNhan.DisplayMember = "Ten";
                cmbNoiNhan.ValueMember = "ID";
                cmbNoiNhan.Enabled = false;
                return;
            }

            HeThongDTO tmp = (HeThongDTO)cmbHeThong.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDiSearchDTO.HeThong = null;
                tmpNoiNhan = new List<NoiNhanDTO>();
                tmpNoiNhan.Insert(0, new NoiNhanDTO(0, "<Tìm tất cả>", null, 0));
                cmbNoiNhan.DataSource = tmpNoiNhan;
                cmbNoiNhan.DisplayMember = "Ten";
                cmbNoiNhan.ValueMember = "ID";
                cmbNoiNhan.Enabled = false;
                return;
            }

            _congVanDiSearchDTO.HeThong = tmp;
            tmpNoiNhan = NoiNhanBUS.LoadAll(tmp.ID, false);
            tmpNoiNhan.Insert(0, new NoiNhanDTO(0, "<Tìm tất cả>", null, 0));
            cmbNoiNhan.DataSource = tmpNoiNhan;
            cmbNoiNhan.DisplayMember = "Ten";
            cmbNoiNhan.ValueMember = "ID";
            cmbNoiNhan.Enabled = true;

        }

        private void cmbNoiNhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNoiNhan.SelectedIndex == -1)
            {
                _congVanDiSearchDTO.NoiNhan = null;
                return;
            }

            NoiNhanDTO tmp = (NoiNhanDTO)cmbNoiNhan.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDiSearchDTO.NoiNhan = null;
                return;
            }
            _congVanDiSearchDTO.NoiNhan = tmp;
        }

        private void cmbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbLoai.SelectedIndex == -1)
            {
                _congVanDiSearchDTO.Loai = null;
                return;
            }

            LoaiDiDTO tmp = (LoaiDiDTO)cmbLoai.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDiSearchDTO.Loai = null;
                return;
            }
            _congVanDiSearchDTO.Loai = tmp;
        }

        private void cmbNguoiKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbNguoiKy.SelectedIndex == -1)
            {
                _congVanDiSearchDTO.NguoiKy = null;
                return;
            }

            NguoiDTO tmp = (NguoiDTO)cmbNguoiKy.SelectedItem;
            if (tmp.ID == 0)
            {
                _congVanDiSearchDTO.NguoiKy = null;
                return;
            }
            _congVanDiSearchDTO.NguoiKy = tmp;
        }

        private void txtNoiDung_TextChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.NoiDung = txtNoiDung.Text;
        }

        private void txtGhiChu_TextChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.GhiChu = txtGhiChu.Text;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null && (dataGridView1.CurrentRow.Index >= 0 || dataGridView1.CurrentRow.Index < dataGridView1.RowCount))
            {
                btnXemChiTiet.Enabled = true;
                btnChinhSua.Enabled = true;
            }
            else
            {
                btnXemChiTiet.Enabled = false;
                btnChinhSua.Enabled = false;
            }
        }

        private void txtSoKiHieu_TextChanged(object sender, EventArgs e)
        {
            _congVanDiSearchDTO.SoKyHieuVanBan = txtSoKyHieu.Text;

            //dataGridView1.Columns.Add(
        }


    }

}
